#pragma once

#include "../../Forest/TreeNode.h"
#include "Terrain_Heightmap.h"
#include "../../Base/vertex.h"
class NxHeightField;
class NxActor;

class Terrain: public TreeNode
{
public:
	Terrain(UINT vertRows, UINT vertCols, float dx, float dz, const tstring& FileName_HeightMap, 
		const tstring& tex0, const tstring& tex1, const tstring& tex2, const tstring& blendMap, float heightScale, 
		float HeightOffset);
	Terrain(const tstring& iniFilename);
	virtual ~Terrain();

	void Draw();
	void DrawShadow() {}
	float GetHeight(float x, float y) const;

private:
	Terrain_Heightmap* m_HeightMap;
	vector<AABB> m_SubGridBndBoxes;

	DWORD m_VertRows;
	DWORD m_VertCols;
	int m_NumBits;
	float m_WidthInUnits;
	float m_DepthInUnits;

	float m_ScaleX;
	float m_ScaleZ;
	float m_ScaleY;

	ID3D10Buffer* m_VertexBuffer,* m_IndexBuffer;
	ID3D10InputLayout* m_Layout;
	ID3D10ShaderResourceView* m_Texture, *m_Texture2, *m_Texture3, *m_Blend;

	ID3D10Effect* m_Effect;
	ID3D10EffectTechnique* m_Tech;
	ID3D10EffectMatrixVariable* m_WorldVar, *m_ViewInvVar, *m_WVPVar, *m_WorldInvVar;
	ID3D10EffectShaderResourceVariable* m_TextureVar, *m_Texture2Var, *m_Texture3Var, *m_BlendVar;

	void Init(const tstring& FileName_HeightMap, const tstring& tex0, const tstring& tex1, 
		const tstring& tex2, const tstring& blendMap, float heightScale, 
		float HeightOffset);
	void Cleanup();
	void BuildGridGeometry();
	void GenTriGrid(int numVertRows, int numVertCols,	float dx, float dz, 
		const D3DXVECTOR3& center, vector<D3DXVECTOR3>& verts,
		vector<DWORD>& indices);
	void BuildVertexDeclaration();
	void buildSubGridMesh(RECT& R, VertexPNT* gridVerts); 
	void LoadShader();

	void InitPhysX();
	NxHeightField* m_HeightField ;
	NxActor *m_Actor;

	Terrain (const Terrain &t);
	Terrain& operator= (const Terrain &t);
};